import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Student{
    //成员变量/字段
    public int age;
    private String name;

    //构造方法
    public Student(int age,String name){
        this.age = age;
        this.name = name;
    }
    public Student(){

    }

    //不带参数的func
    public void func(){
        System.out.println("获取到不带参数的func方法");
    }

    //带参数的func方法
    public void func(String pragma){
        System.out.println(pragma + "获取到带参数的func方法");
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

}


public class reflectionDemo {

    public static void main(String[] args){
        try{
            //获取反射的类对象
            Class<?> student = Class.forName("Student");

            //需要反射的字段
            Field field1 = student.getField("age");
            Field field2 = student.getDeclaredField("name");

            //设置可修改
            field1.setAccessible(true);
            field2.setAccessible(true);

            //实例化对象
            Student s = (Student) student.newInstance();

            //调用set方法
            field1.set(s,20);
            field2.set(s,"lirays");

            //输出
            System.out.println(s);

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main3(String[] args)  {
        try{
            //获取反射类对象
            Class<?> student = Class.forName("Student");

            //获取方法，带参数的func
            Method method = student.getMethod("func",String.class);
            //获取不带参数的func
            Method method1 = student.getMethod("func");

            //设置可以修改或者获取
            method.setAccessible(true);
            //设置为可修改
            method1.setAccessible(true);

            //实例化对象引用
            Student s = (Student) student.newInstance();

            //调用方法，s的反射类的引用，‘gg‘是给func方法传参
            method.invoke(s,"gg");
            method1.invoke(s);
            
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main2(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class<?> student = Class.forName("Student");

        Constructor<?> constructor = student.getConstructor(int.class,String.class);

        constructor.setAccessible(true);

        //调用newInstance方法实例化对象
        Student s1 = (Student) constructor.newInstance(18,"李四");
        //输出
        System.out.println(s1);
    }

    public static void main1(String[] args) throws ClassNotFoundException {
        Class<?> stu1 = Class.forName("Student");

        Class<?> stu2 = Student.class;

        Student s = new Student(10,"lisi");
        Class<?> stu3 = s.getClass();

        System.out.println(stu2 == stu1);
        System.out.println(stu1 == stu3);
        System.out.println(stu2 == stu3);
    }
}
